package com.ruoyi.project.relation.domain.dto;

import org.shoukaiseki.common.exception.MessageVirtualException;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 *
 **/
public class BusinessRelationDO {

    @NotBlank(message = "ownerName 不能为空")
    private String ownerName;

    @NotBlank(message = "ownerType 不能为空")
    private String ownerType;

    @NotNull(message = "ownerId 不能为空")
    private Long ownerId;


    /**
     * 不推荐使用该方式,只为spring构造
     */
    public BusinessRelationDO() {

    }

    public BusinessRelationDO(String ownerName, String ownerType, Long ownerId) {
        this.ownerName = ownerName;
        this.ownerType = ownerType;
        this.ownerId = ownerId;
    }

    public Long getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(Long ownerId) {
        this.ownerId = ownerId;
    }

    public String getOwnerName() {
        return ownerName;
    }

    public void setOwnerName(String ownerName) {
        this.ownerName = ownerName;
    }

    public String getOwnerType() {
        return ownerType;
    }

    public void setOwnerType(String ownerType) {
        this.ownerType = ownerType;
    }

    /**
     * 任何一个参数都不能为空
     * @param args   ownerName,ownerType,ownerId.....ownerName,ownerType,ownerId
     * @return
     */
    public static BusinessRelationDO[] newBusinessRelationDO(Object... args){
        if ((args.length % 3) != 0) { // implicit nullcheck of input
            throw new InternalError("参数个数必须为3的倍数");
        }
        Set<BusinessRelationDO> sets=new HashSet<>();
        for (int i = 0; i < args.length; i += 3) {
            @SuppressWarnings("unchecked")
            String ownerName = Objects.requireNonNull(args[i]).toString();
            String ownerType = Objects.requireNonNull(args[i+1]).toString();
            Object ownerId = args[i+2];
            if(!(ownerId instanceof Long)){
                throw new MessageVirtualException("类型必须为 Long;index={},v={}").format(i,ownerId);
            }
            sets.add(new BusinessRelationDO(ownerName,ownerType,(Long)ownerId));
        }
        return sets.stream().toArray(BusinessRelationDO[]::new);
    }


}

